package com.mentaleye.service.impl;

import com.mentaleye.bean.GroupPersonBean;
import com.mentaleye.dal.dao.GroupPersonDao;
import com.mentaleye.dal.model.GroupPerson;
import com.mentaleye.dal.model.GroupPersonExample;
import com.mentaleye.dal.model.GroupPersonKey;
import com.mentaleye.service.GroupPersonService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Rock on 2017/5/10.
 */
@Service
public class GroupPersonServiceImpl implements GroupPersonService{

    @Autowired private GroupPersonDao groupPersonDao;

    public boolean add(GroupPersonBean bean){
        return groupPersonDao.add((GroupPerson) ObjectUtil.transfer(bean ,GroupPerson.class));
    }

    public boolean update(GroupPersonBean bean){
        return groupPersonDao.update((GroupPerson) ObjectUtil.transfer(bean ,GroupPerson.class));
    }

    public boolean updateByPersonId(GroupPersonBean bean){
        return groupPersonDao.updateByPersonId((GroupPerson) ObjectUtil.transfer(bean ,GroupPerson.class));
    }

    public boolean remove(GroupPersonKey key){
        return groupPersonDao.delete(key);
    }

    public boolean removeByGroupId(Integer groupId){
        GroupPersonExample example = new GroupPersonExample();
        example.createCriteria().andGroupIdEqualTo(groupId);
        return groupPersonDao.deleteByExample(example);
    }

    public boolean removeList(Map<String,Object> map){
        return groupPersonDao.deleteGroupPersonList(map);
    }

    public GroupPersonBean query(GroupPersonKey key){
        return (GroupPersonBean) ObjectUtil.transfer(groupPersonDao.select(key) ,GroupPersonBean.class);
    }

    public List<GroupPersonBean> queryByGroupId(Integer groupId){
        GroupPersonExample example = new GroupPersonExample();
        example.createCriteria().andGroupIdEqualTo(groupId);
        return (List<GroupPersonBean>) ObjectUtil.transferList(groupPersonDao.selectByExample(example),GroupPersonBean.class);
    }

    public void queryByCommpage(CommonPage commonPage){
        List<Map<String, Object>> data = groupPersonDao.selectByMap(commonPage.pageToMap());
        commonPage.setPageData(data);
    }

    public List<Map<String, Object>> queryByMap(Map<String,Object> map){
        return groupPersonDao.selectByMap(map);
    }

    public List<Map<String, Object>> queryByNotInClass(Integer classId){
        return groupPersonDao.selectByNoClass(classId);
    }

    public List<Map<String, Object>> queryPersonList(Integer oaclassId, boolean have){
        if(have){
            return groupPersonDao.selectPersonByClassId(oaclassId);//查询已分组学员
        }
        return groupPersonDao.selectPersonNotGroup(oaclassId);//查询未分组学员
    }
}
